use env_logger::{Env, Target};
use shimmer_club::app::Application;
use std::{env, io::Result as IOResult};
/// ## 帮助文本
static HELP_TEXT: &str = r"shimmer_club v0.2.0 help:

  --out-config [path]       输出默认配置文件到[path]
  -h --help                 输出帮助信息
";

#[actix_web::main]
async fn main() -> IOResult<()> {
    env_logger::builder()
        .parse_env(Env::default().default_filter_or("info"))
        .target(Target::Stdout)
        .try_init()
        .unwrap();

    {
        let mut args = std::env::args();
        while let Some(param_key) = args.next() {
            if param_key == "--out-config" {
                // 输出默认配置
                let out_path = args.next().unwrap_or_else(|| "config.json".into());
                log::info!("输出默认配置到 {}", out_path);
                std::fs::write(
                    out_path,
                    serde_json::to_string_pretty(&Application::get_default_config()).unwrap(),
                )
                .unwrap();
                log::info!("输出成功");
                return Ok(());
            }
            if param_key == "-h" || param_key == "--help" {
                println!("{}", HELP_TEXT);
                return Ok(());
            }
        }
    }
    let config_path = env::var("CONFIG_PATH").unwrap_or_else(|_| "config.json".to_string());
    log::info!("shimme_club 准备启动!!!");
    log::info!("配置文件路径为:\"{}\"", config_path);
    Application::new()
        .set_config_path(Some(config_path))
        .start()
        .await
        .unwrap();

    Ok(())
}
